<?php

/* 动态--举报管理 */
namespace app\admin\controller;

use app\admin\service\ReportClassifyService;
use cmf\controller\AdminBaseController;
use think\facade\Db;

class DynamicreportController extends AdminBaseController
{
	//**************举报类型设置*****************************
	//举报类型列表
	function classindex(){
        $params = $this->request->param();
        $params['type'] = 3;
        if (empty($params['langId'])) {
            $params['langId'] = $this->defaultLanguage['id'];
            $this->assign('langId', $this->defaultLanguage['id']);
        }else {
            $this->assign('langId', $params['langId']);
        }
        $list = ReportClassifyService::getData($params);
//		$list = Db::name('dynamic_report_classify')
//            ->order("list_order asc")
//            ->paginate(20);
//
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign('list', $list);
        $this->assign('countryData', $this->recommendLanguageData);
        return $this->fetch();
	}
	
	public function add()
    {
        $this->assign('countryData', $this->recommendLanguageData);
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();
            
            $name=$data['name'];
            $data['type'] = 3;
            
            if($name == ''){
                $this->error('请填写名称');
            }else{
                $condition = [
                    'del_flag' => 0,
                    'name'=>$name,
                    'lang_id'=>$data['lang_id'],
                    'type' => 3
                ];
                $check=Db::name('user_report_classify')->where($condition)->find();
                if($check){
                    $this->error('名称已存在');
                }
            }

            $id = DB::name('user_report_classify')->insertGetId($data);
            if(!$id){
                $this->error("添加失败！");
            }
            $this->resetcache();
            $this->success("添加成功！");
        }
    }
	
	 public function edit()
    {
        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('user_report_classify')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();
            $classData = Db::name('user_report_classify')->where(['id' => $data['id']])->find();
            $id=$data['id'];
            $name=$data['name'];
            if($name == ''){
                $this->error('请填写名称');
            }else{
                $condition = [
                    'del_flag' => 0,
                    'name'=>$name,
                    'type' => 3,
                    'lang_id' => $classData['lang_id'],
                ];
                $check = Db::name('user_report_classify')->where($condition)->where("id !='{$id}'")->find();
                if($check){
                    $this->error('名称已存在');
                }
            }
            $rs = DB::name('user_report_classify')->update($data);

            if($rs === false){
                $this->error("保存失败！");
            }
            $this->resetcache();
            $this->success("保存成功！");
        }
    }
	
	public function listOrder()
    {
        $model = DB::name('dynamic_report_classify');
        parent::listOrders($model);
        $this->resetcache();
        $this->success("排序更新成功！");
    }

    public function del()
    {
        $id = $this->request->param('id', 0, 'intval');
        
        $rs = DB::name('user_report_classify')->where("id={$id}")->update(['del_flag' => 1]);
        if(!$rs){
            $this->error("删除失败！");
        }
        $this->resetcache();
        $this->success("删除成功！",url("dynamicreport/classindex"));
    }


    protected function resetcache(){
        $key='dynamic_report';

        $level=DB::name('user_report_classify')
                ->where('type',3)
                ->field('id,name,name_cn')
                ->order("list_order asc")
                ->select();
        if($level){
            setcaches($key,$level);
        }
    }
	
	//**************举报类型设置结束**************************
	//动态举报列表
    public function index()
    {
        
        $data = $this->request->param();
        $map=[];
		$start_time=isset($data['start_time']) ? $data['start_time']: '';
        $end_time=isset($data['end_time']) ? $data['end_time']: '';
        if($start_time!=""){
           $map[]=['addtime','>=',strtotime($start_time)];
        }
        if($end_time!=""){
           $map[]=['addtime','<=',strtotime($end_time) + 60*60*24];
        }
		
		$status=isset($data['status']) ? $data['status']: '';
        if($status!=''){
            $map[]=['status','=',$status];
        }
		


        $uid=isset($data['uid']) ? $data['uid']: '';
        if($uid!=''){
            $map[]=['uid','=',$uid];
        }
		
		$touid=isset($data['touid']) ? $data['touid']: '';
        if($touid!=''){
            $map[]=['touid','=',$touid];
        }

        $list = Db::name('dynamic_report')
            ->where($map)
            ->order("id desc")
            ->paginate(20);
	
        $list->each(function($v, $k){
            $userinfo=getUserInfo($v['uid']);
            $v['userinfo']=$userinfo;
			$touserinfo=getUserInfo($v['touid']);
			$v['touserinfo']=$touserinfo;
            
           
            return $v;
        });
    
        $page = $list->render();
        $this->assign("page", $page);
            
        $this->assign('list', $list);
       


        return $this->fetch();
    }
	
	 //设置下架
    public function setXiajia(){
    	$res=array("code"=>0,"msg"=>"下架成功","info"=>array());
		$data      = $this->request->param();
            
		$id=$data['id'];
		$reason=$data['reason'];
		
    	if(!$id){
    		$res['code']=1001;
    		$res['msg']="请确认动态信息";
    		echo json_encode($res);
    		exit;
    	}

    	//判断此动态是否存在
    	$dynamicInfo=Db::name("dynamics")->where("id={$id}")->find();
    	if(!$dynamicInfo){
    		$res['code']=1001;
    		$res['msg']="请确认动态信息";
    		echo json_encode($res);
    		exit;
    	}

    	//更新动态状态
    	$data=array("isdel"=>1,"xiajia_reason"=>$reason);

    	$result=Db::name("dynamics")->where("id={$id}")->update($data);

    	if($result!==false){

    		//将动态喜欢列表的状态更改
    		Db::name("dynamics_like")->where("dynamicid={$id}")->update(['status'=>0]);

    	
    		//更新此动态的举报信息
    		$data1=array(
    			'status'=>1,
    			'uptime'=>time()
    		);

    		Db::name("dynamic_report")->where("dynamicid={$id}")->update($data1);

    		$user_id=session('ADMIN_ID');
		
			
			$user=Db::name("User")->where("id='{$user_id}'")->find();
			$ip=get_client_ip(0, true);
            
            $ip=ip2long($ip);
    		//向系统通知表中写入数据
    		$sysInfo=array(
    			// 'title'=>'动态下架提醒',
    			'addtime'=>time(),
    			'admin'=>$user['user_login'],
    			'ip'=>$ip,
    			'touid'=>$dynamicInfo['uid']

    		);


    		if($dynamicInfo['title']!=''){
    			$videoTitle='上传的《'.$dynamicInfo['title'].'》';
    		}else{
    			$videoTitle='上传的';
    		}

    		$baseMsg='您于'.date("Y-m-d H:i:s",$dynamicInfo['addtime']).$videoTitle.'动态被管理员于'.date("Y-m-d H:i:s",time()).'下架';;

    		if(!$reason){
    			$sysInfo['content']=$baseMsg;
    		}else{
    			$sysInfo['content']=$baseMsg.',下架原因为：'.$reason;
    		}

    		$result1=Db::name("sys_notice")->insertGetId($sysInfo);
			
			/* IM */
    		/*if($result1!==false){
				$ext=[
					'method'=>'sysnotice',
					'action'=>'0',
					'content'=>$content,
				];
				$configpri=getConfigPri();
				$group_id=$configpri['im_full_group_id'];
				$text_content=json_encode($ext);
				$api=getTxRestApi();
				$ret = $api->group_send_group_system_notification($group_id, $text_content, $receiver_id);
				
    		}*/
    		echo json_encode($res);
    		exit;
    	}else{
    		$res['code']=1002;
    		$res['msg']="下架失败";
    		echo json_encode($res);
    		exit;
    	}
    	
    }
	//查看视频
	public function view()
    {
        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('dynamics')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        
        $data['href']=get_upload_path($data['href']);
        
        $this->assign('data', $data);
        return $this->fetch();
    }
	//播放动态语音
	public function playyy()
    {
        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('dynamics')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        $data['voice']=get_upload_path($data['voice']);
    
        $this->assign('data', $data);
        return $this->fetch();
    }
	//查看图片集合
	 public function look()
    {
        $id   = $this->request->param('id', 0, 'intval');
        $dynamicid   = $this->request->param('dynamicid', 0, 'intval');
 
        $datady=Db::name('dynamics')
            ->where("id={$dynamicid}")
            ->find();
        if(!$datady){
            $this->error("动态不存在");
        }
		if($datady['thumb']){
			$thumbs=explode(";",$datady['thumb']);
			foreach($thumbs as $kk=>$vv){
				$thumbs[$kk]=get_upload_path($vv);
			}
			$this->assign('thumbs', $thumbs);			
		}
        

        return $this->fetch();
    }
	//删除举报
	public function report_del()
    {
        $id = $this->request->param('id', 0, 'intval');
        
        $rs = DB::name('dynamic_report')->where("id={$id}")->delete();
        if(!$rs){
            $this->error("删除失败！");
        }
        $this->resetcache();
        $this->success("删除成功！",url("dynamicreport/index"));
    }

}